import { Component, Mixins } from 'vue-property-decorator'
import { consts } from '@yolanda-qn/eight-electrodes-report-lib'
import model from './model'
// @ts-ignore
import { getUrlQuery } from '@/utils/helper'

const { LangsEnum, WeightUnitEnum } = consts

export function isNumeric(value: unknown): value is string | number {
  // 数组会有隐式转换所以要排除
  return !Array.isArray(value) && /^[+-]?[0-9]\d*$|^[+-]?[0-9]\d*\.\d+$/g.test(value as string);
}

@Component({})
export default class extends Mixins(model) {
  async init() {
    this.getReportDetail()
    if (!this.config.lang) {
      this.config.lang = LangsEnum.en
    }
    if (!this.config.fallbackLang) {
      this.config.fallbackLang = LangsEnum.en
    }
    if (!this.config.weightUnit) {
      this.config.weightUnit = WeightUnitEnum.kg;
    }
    const { lang, weightUnit, fallbackLang } = this.config
    this.i18n.setLang(lang)
    this.i18n.setFallbackLang(fallbackLang)
    this.weight_unit = weightUnit
  }
  /**
   * 获取测量数据和配置
   */
  getReportDetail() {
    const queries = getUrlQuery(window.location.href);
    const measureData = JSON.parse(queries.measureData)
    // 转为数字
    Object.entries(measureData).forEach(([k, v]) => {
      if (isNumeric(v)) {
        measureData[k] = Number(v);
      }
    });
    this.config = JSON.parse(queries.config || '{}')
    this.measureData = measureData
  }
}
